@Override
public void exportWord(PbpGroupCostDto resources, HttpServletRequest request, HttpServletResponse response) throws IOException {
Map groupCost = new HashMap();
response.setContentType("application/octet-stream");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
String reportDate = sdf.format(resources.getDate());
String filename = resources.getGroupName()+reportDate+".docx";
ImageEntity image = new ImageEntity();
image.setHeight(50);
image.setWidth(150);
String sourceFilePath = getUploadFilePathByLocation(resources.getSignatureUpImg()); // 源文件的磁盘位置
image.setData(image2byte(sourceFilePath));
image.setType(ImageEntity.Data);
groupCost.put("img1", image);
ExportWordUtils.exportWord("template/tourFeeReport.docx","D:/groupCost",filename,groupCost,request,response);
}
//使用Url到不出来,只有转成base64字节数组才能导出
//图片到byte数组
public byte[] image2byte(String path){
byte[] data = null;
FileImageInputStream input = null;
try {
input = new FileImageInputStream(new File(path));
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int numBytesRead = 0;
while ((numBytesRead = input.read(buf)) != -1) {
output.write(buf, 0, numBytesRead);
}
data = output.toByteArray();
output.close();
input.close();
}
catch (FileNotFoundException ex1) {
ex1.printStackTrace();
}
catch (IOException ex1) {
ex1.printStackTrace();
}
return data;
}
四、注意点本人使用的是新版本的easypoi该版本貌似不支持setUrl导出,只支持base64位码导出。最好使用新版的easypoi因为新版本支持{{fe:list }}横向和纵向循环。
|